package com.lin.annotationtest;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.lin.annotationtest.annotations.Column;
import com.lin.annotationtest.annotations.Table;

public class BaseEntity {
	private  Logger logger = LoggerFactory.getLogger(this.getClass());
	public void printAnnotationMessage() {
		StringBuffer sb = new StringBuffer("*********\n");
		
		Class thisClass = this.getClass();
		if(thisClass.isAnnotationPresent(Table.class)){
			Table table =(Table) thisClass.getAnnotation(Table.class);
			sb.append("Table:"+table.value()+ "\n");
		}
		
		Field[] fieldsArr = thisClass.getDeclaredFields();
		for (Field field : fieldsArr) {
			if(field.isAnnotationPresent(Column.class)){
				Column column = (Column) field.getAnnotation(Column.class);
				sb.append(column.value()+"：");
				Object columnValue = null;
				field.setAccessible(true);
				try {
					columnValue = field.get(this);
					sb.append(columnValue+"\n");
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(sb.toString());
		logger.info("****************************printAnnotationMessage");
	}
}
